//
//  JSViewController50.swift
//  RXSwiftTest
//
//  Created by ZhangBo on 2019/6/19.
//  Copyright © 2019 ZhangBo. All rights reserved.
//

import UIKit

import RxSwift
import RxCocoa
import RxDataSources
import Alamofire
import RxAlamofire


class JSViewController50: BaseViewController {
    let disposeBag = DisposeBag()
    //显示频道列表的tableView
    var tableView: UITableView!

    override func viewDidLoad() {
        super.viewDidLoad()
        test6()
    }
}

extension JSViewController50 {
    fileprivate func test1() {
        //获取数据
        DouBanProvider.rx.request(.channels).subscribe(onSuccess: { response in
            //数据处理
            let json = try? response.mapJSON() as! [String: Any]
            print("--- 请求成功！返回的如下数据 ---")
            print(json!)
        }, onError: { error in
            print("数据请求失败!错误原因：", error)
        }).disposed(by: disposeBag)
    }

    fileprivate func test2() {
        //获取数据
        DouBanProvider.rx.request(.channels).mapJSON().subscribe(onSuccess: { data in
            //数据处理
            let json = data as! [String: Any]
            print("--- 请求成功！返回的如下数据 ---")
            print(json)
        }, onError: { error in
            print("数据请求失败!错误原因：", error)

        }).disposed(by: disposeBag)
    }
}


extension JSViewController50 {
    fileprivate func test3() {
        //创建表视图
        self.tableView = UITableView(frame: self.view.frame, style: .plain)
        //创建一个重用的单元格
        self.tableView!.register(UITableViewCell.self, forCellReuseIdentifier: "Cell")
        self.view.addSubview(self.tableView!)

        //获取列表数据
        let data = DouBanProvider.rx.request(.channels)
            .mapJSON()
            .map { data -> [[String: Any]] in
                if let json = data as? [String: Any],
                   let channels = json["channels"] as? [[String: Any]] {
                    return channels
                } else {
                    return []
                }
            }.asObservable()

        //将数据绑定到表格
        data.bind(to: tableView.rx.items) { (tableView, row, element) in
            let cell = tableView.dequeueReusableCell(withIdentifier: "Cell")!
            cell.textLabel?.text = "\(element["name"]!)"
            cell.accessoryType = .disclosureIndicator
            return cell
        }.disposed(by: disposeBag)

        //单元格点击
        tableView.rx.modelSelected([String: Any].self)
            .map {
                $0["channel_id"] as! String
            }
            .flatMap {
                DouBanProvider.rx.request(.playlist($0))
            }
            .mapJSON().subscribe(onNext: { [weak self] data in
                //解析数据，获取歌曲信息
                if let json = data as? [String: Any],
                   let musics = json["song"] as? [[String: Any]] {
                    print(musics)
                }
            }).disposed(by: disposeBag)
    }

    fileprivate func test4() {
        //获取数据
        DouBanProvider.rx.request(.channels).mapObject(Douban.self).subscribe(onSuccess: { douban in
                if let channels = douban.channels {
                    print("--- 共\(channels.count)个频道 ---")
                    for channel in channels {
                        if let name = channel.name, let channelId = channel.channelId {
                            print("\(name) （id:\(channelId)）")
                        }
                    }
                }
            }, onError: { error in
                print("数据请求失败!错误原因：", error)
            })
            .disposed(by: disposeBag)
    }
}

extension JSViewController50 {
    fileprivate func test5() {
        //创建表视图
        self.tableView = UITableView(frame: self.view.frame, style: .plain)
        //创建一个重用的单元格
        self.tableView!.register(UITableViewCell.self, forCellReuseIdentifier: "Cell")
        self.view.addSubview(self.tableView!)

        //获取列表数据
        let data = DouBanProvider.rx.request(.channels)
            .mapObject(Douban.self)
            .map {
                $0.channels ?? []
            }
            .asObservable()

        //将数据绑定到表格
        data.bind(to: tableView.rx.items) { (tableView, row, element) in
            let cell = tableView.dequeueReusableCell(withIdentifier: "Cell")!
            cell.textLabel?.text = "\(element.name!)"
            cell.accessoryType = .disclosureIndicator
            return cell
        }.disposed(by: disposeBag)

        //单元格点击
        tableView.rx.modelSelected(Channel.self)
            .map {
                $0.channelId!
            }
            .flatMap {
                DouBanProvider.rx.request(.playlist($0))
            }
            .mapObject(Playlist.self)
            .subscribe(onNext: { [weak self] playlist in
                //解析数据，获取歌曲信息
                if playlist.song.count > 0 {
                    let artist = playlist.song[0].artist!
                    let title = playlist.song[0].title!
                    let message = "歌手：\(artist)\n歌曲：\(title)"
                    //将歌曲信息弹出显示
                    self?.showAlert(title: "歌曲信息", message: message)
                }
            }).disposed(by: disposeBag)
    }

    fileprivate func test6() {
        //创建表视图
        self.tableView = UITableView(frame: self.view.frame, style: .plain)
        //创建一个重用的单元格
        self.tableView!.register(UITableViewCell.self, forCellReuseIdentifier: "Cell")
        self.view.addSubview(self.tableView!)

        //豆瓣网络请求服务
        let networkService = DouBanNetworkService()

        //获取列表数据
        let data = networkService.loadChannels()

        //将数据绑定到表格
        data.bind(to: tableView.rx.items) { (tableView, row, element) in
            let cell = tableView.dequeueReusableCell(withIdentifier: "Cell")!
            cell.textLabel?.text = "\(element.name!)"
            cell.accessoryType = .disclosureIndicator
            return cell
        }.disposed(by: disposeBag)

        //单元格点击
        tableView.rx.modelSelected(Channel.self)
            .map {
                $0.channelId!
            }
            .flatMap(networkService.loadFirstSong)
            .subscribe(onNext: { [weak self] song in
                //将歌曲信息弹出显示
                let message = "歌手：\(song.artist!)\n歌曲：\(song.title!)"
                self?.showAlert(title: "歌曲信息", message: message)
            }).disposed(by: disposeBag)
    }

}
